package com.eobard.utils;

import cn.hutool.core.util.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import javax.xml.xpath.XPathConstants;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: divenswu
 * @Date: 2025-04-24
 * @Description: 安卓R资源文件处理
 */
@Slf4j
public class AndroidRFileUtils {
    public static String getPackageName(String sourceRoot) {
        try {
            String sourceRootParent = Paths.get(sourceRoot).getParent().toString();
            Path manifest = Paths.get(sourceRootParent, "AndroidManifest.xml");
            if (manifest.toFile().exists()) {
                String packageXpath = "//manifest/@package";
                Document manifestXml = XmlUtil.readXML(manifest.toFile());
                Object packageName = XmlUtil.getByXPath(packageXpath, manifestXml, XPathConstants.STRING);
                if (null != packageName) {
                    return packageName.toString();
                }
            }
        }catch (Exception e){
            log.error("生成安卓R文件失败:",e);
        }
        return null;
    }

    public static String getRFileContent(String sourceRoot,String pgn) {
        String sourceRootParent = Paths.get(sourceRoot).getParent().toString();
        Path manifest = Paths.get(sourceRootParent, "AndroidManifest.xml");
        if (manifest.toFile().exists()) {
            String packageXpath = "//manifest/@package";
            Document manifestXml = XmlUtil.readXML(manifest.toFile());
            Object packageName = XmlUtil.getByXPath(packageXpath, manifestXml, XPathConstants.STRING);
            if (null != packageName) {
                List<String> rawFields = new ArrayList<>();
                File rawPath = Paths.get(sourceRootParent, "res/raw").toFile();
                if (rawPath.exists()) {
                    String[] rawFiles = rawPath.list();
                    if (null != rawFiles) {
                        for (String rawFile : rawFiles) {
                            rawFields.add("\t\tpublic static final int " + rawFile.split("\\.")[0] + " = 0;");
                        }
                    }
                }
                List<String> layoutFields = new ArrayList<>();
                File layoutPath = Paths.get(sourceRootParent, "res/layout").toFile();
                if (layoutPath.exists()) {
                    String[] layoutFiles = layoutPath.list();
                    if (null != layoutFiles) {
                        for (String layoutFile : layoutFiles) {
                            layoutFields.add("\t\tpublic static final int " + layoutFile.split("\\.")[0] + " = 0;");
                        }
                    }
                }

                List<String> valueFields = new ArrayList<>();
                File valuePath = Paths.get(sourceRootParent, "res/values").toFile();
                if (valuePath.exists()) {
                    String[] valueFiles = valuePath.list();
                    if (null != valueFiles) {
                        for (String valueFile : valueFiles) {
                            Document valueXml = XmlUtil.readXML(Paths.get(valuePath.getPath(), valueFile).toFile());
                            NodeList nodeList = XmlUtil.getNodeListByXPath("//resources/string", valueXml);
                            for (int i = 0; i < nodeList.getLength(); i++) {
                                Object name = XmlUtil.getByXPath("@name", nodeList.item(i), XPathConstants.STRING);
                                valueFields.add("\t\tpublic static final int " + name.toString() + " = 0;");
                            }
                        }
                    }
                }
                return "" +
                        //android.os
                        "package "+pgn+";\n\n" +
                        "public class R {\n" +
                        "    public static final class raw {\n" +
                        String.join("\n", rawFields) + "\n" +
                        "    }\n" +
                        "    public static final class string {\n" +
                        String.join("\n", valueFields) + "\n" +
                        "    }\n" +
                        "    public static final class layout {\n" +
                        String.join("\n", layoutFields) + "\n" +
                        "    }\n" +
                        "    public static final class drawable {\n\n" +
                        "    }\n" +
                        "}";
            }
        }
        return null;
    }
}
